﻿//-----------------------------------------------------------------------
// <copyright file="VariablesProcessor.cs" company="Erik Lieben">
//     Copyright Erik Lieben. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace ErikLieben.CssPreprocessor
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Text.RegularExpressions;

    /// <summary>
    /// Class that processes the css for variables
    /// Works for now....
    /// TODO: Figure out how to nicely layout this stuff
    /// </summary>
    public static class VariablesProcessor
    {
        /// <summary>
        /// Processes the variables in a given string.
        /// </summary>
        /// <param name="cssCode">The CSS code.</param>
        /// <param name="variables">The variable dictionary with possible existing variables.</param>
        /// <returns>a dictionary of variables found</returns>
        public static Dictionary<string, string> Process(string cssCode, Dictionary<string, string> variables)
        {
            // Nothing to process? then return.
            if (string.IsNullOrWhiteSpace(cssCode))
            {
                return variables;
            }

            foreach (Match match in Regex.Matches(cssCode, "@variable (?<name>.*?)\\{(?<inner>.*?)\\}"))
            {
                string variableName = match.Groups["name"].Value.Trim();
                string[] subVars = match.Groups["inner"].Value.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                // say what?
                if (subVars.Length < 1)
                {
                    Trace.WriteLine("Error parsing variable " + variableName);
                    continue;
                }

                // single variable definition
                if (subVars.Length == 1)
                {
                    if (variables.ContainsKey(variableName))
                    {
                        variables[variableName] = subVars[0];
                    }
                    else
                    {
                        variables.Add(variableName, subVars[0]);
                    }
                }
                else
                {
                    // multi variable definition
                    foreach (string subVar in subVars)
                    {
                        string[] subVarDef = subVar.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                        
                        // what?
                        if (subVarDef.Length < 1) 
                        {
                            Trace.WriteLine("Error parsing multi variable string " + subVars);
                            continue;
                        }

                        string key = variableName + "." + subVarDef[0];
                        if (variables.ContainsKey(key))
                        {
                            variables[key] = subVarDef[1].Trim();
                        }
                        else
                        {
                            variables.Add(key, subVarDef[1].Trim());
                        }
                    }
                }
            }

            return variables;
        }
    }
}
